<?php

namespace App\Listeners\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\UserBaseInfoEnum;
use App\Enums\OpenShop\InvestmentPromotion\UserIdentityCardEnum;
use App\Events\OpenShop\OrderCreatedEvent;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserBaseInfoModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserExperienceModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserIdentityCardModel;

class OrderCreatedLogListener extends BaseListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Handle the event.
     *
     * @param OrderCreatedEvent $event
     * @return void
     */
    public function handle(OrderCreatedEvent $event)
    {
        $userIdentityCard = OpenShopUserIdentityCardModel::getDataByOrderNo($event->order->orderNo);
        $userBaseInfo     = OpenShopUserBaseInfoModel::query()
            ->where('orderNo', $event->order->orderNo)
            ->first();
        $userExperience = OpenShopUserExperienceModel::getDataByOrderNo($event->order->orderNo);
        // 申请人身份信息
        $changes = '';
        if (null !== $userIdentityCard) {
            $changes .= "\n\n🆔用户身份信息：" . PHP_EOL;
            $changes .= "身份证姓名：{$userIdentityCard->name}\n";
            $changes .= "身份证号码：{$userIdentityCard->identityCardNumber}\n";
            $changes .= "性别：" . (UserIdentityCardEnum::GENDER_2 == $userIdentityCard->gender ? '男' : '女') . "\n";
            $changes .= "身份证有效期：" . date('Y-m-d', $userIdentityCard->effectiveStartTime) . ' ~ ' . date('Y-m-d', $userIdentityCard->effectiveEndTime) . "\n";
            $changes .= "籍贯：{$userIdentityCard->nativePlace}\n";
            $changes .= "民族：{$userIdentityCard->nation}\n";
            $changes .= "生日：{$userIdentityCard->birthday}\n";
            $changes .= "生肖：{$userIdentityCard->animalSign}\n";
            $changes .= "星座：{$userIdentityCard->constellation}\n";
        }

        // 申请人基本信息
        if (null !== $userBaseInfo) {
            $source              = UserBaseInfoEnum::SOURCE[$userBaseInfo->source] ?? '-';
            $joinReason          = UserBaseInfoEnum::JOIN_REASON[$userBaseInfo->joinReason] ?? '-';
            $investmentMode      = UserBaseInfoEnum::INVESTMENT_MODE[$userBaseInfo->investmentMode] ?? '-';
            $financeSource       = UserBaseInfoEnum::FINANCE_SOURCE[$userBaseInfo->financeSource] ?? '-';
            $chooseAddressReason = UserBaseInfoEnum::CHOOSE_ADDRESS_REASON[$userBaseInfo->chooseAddressReason] ?? '-';

            $changes .= "\n\n🧑‍🦱用户基本信息：" . PHP_EOL;
            $changes .= "注册手机号:{$userBaseInfo->mobile}\n";
            $changes .= "备注手机号1:{$userBaseInfo->remarkMobile1}\n";
            $changes .= "备注手机号2:{$userBaseInfo->remarkMobile2}\n";
            $changes .= "微信号:{$userBaseInfo->weChatNumber}\n";
            $changes .= "婚姻状况:" . (2 == $userBaseInfo->isMarried ? '已' : '未') . "婚\n";
            $changes .= "三级地址:{$userBaseInfo->addressPrefix}\n";
            $changes .= "详细地址:{$userBaseInfo->address}\n";
            $changes .= "邮编:{$userBaseInfo->postCode}\n";
            $changes .= "来源:{$source}\n";
            $changes .= "亲自经营:" . ($userBaseInfo->isSelfManaged ? '是' : '否') . "\n";
            $changes .= "加盟原因:{$joinReason}\n";
            $changes .= "开店预算:{$userBaseInfo->shopBudget} 万元\n";
            $changes .= "投资方式:{$investmentMode}\n";
            $changes .= "资金来源:{$financeSource}\n";
            $changes .= "自带选址:" . (UserBaseInfoEnum::SELF_WITH_ADDRESS == $userBaseInfo->isSelfWithAddress ? '是' : '否') . "\n";
            $changes .= "意向门店三级地址:{$userBaseInfo->shopAddressPrefix}\n";
            $changes .= "门店选址三级地址:{$userBaseInfo->chooseShopAddressPrefix}\n";
            $changes .= "意向门店名字:{$userBaseInfo->shopName}\n";
            $changes .= "意向门店地址:{$userBaseInfo->shopAddress}\n";
            $changes .= "选址原因:{$chooseAddressReason}\n";
            $changes .= "Email:{$userBaseInfo->email}\n";
        }
        // 身份申请人工作经历
        if (null !== $userExperience) {
            $changes .= "\n\n🧑‍🏫工作经验：" . PHP_EOL;
            $changes .= "是否有餐饮经验：" . ($userExperience->cateringExperience ? '是' : '否') . "\n";
            $changes .= "是否有加盟连锁经验：" . ($userExperience->joinChainExperience ? '是' : '否') . "\n";
            $changes .= "是否有自营经验：" . ($userExperience->employmentExperience ? '是' : '否') . "\n";
            $changes .= "工作年限：{$userExperience->workLife}年\n";
        }
        // 写入
        $log               = new OpenShopSignOrderLogModel;
        $log->orderId      = $event->order->id;
        $log->content      = "创建开店订单";
        $log->type         = OpenShopSignOrderLogModel::TYPE_ORDER_CREATED;
        $log->operatorName = $event->userName;
        $log->createdAt    = strtotime($event->datetime);
        $log->updatedAt    = strtotime($event->datetime);
        $log->createdByUid = $event->userId;
        $log->updatedByUid = $event->userId;
        $log->changes      = trim($changes, "\n");
        $log->save();
    }
}
